We reviewed how to create static plots using ggplot() and introduced interactive documents via html widgets and shiny apps.
Today we’ll learn how to create reproducible documents. By the end of this session, you should be able to:
Ctrl/Cmd+Shift+F10), clear the console (Ctrl/Cmd+L), and clear your workspaceIs your project still open? If not, click on the project icon to load it. (Don’t create a new one.)
Create a new RMarkdown HTML file
Hit the knit button to knit to HTML. Then replace output: html_document in the YAML with output: pdf_document and knit again.
Change options chunk-by-chunk, or set global options:
eval=: FALSE if you do not want R to run (evaluate) the chunkecho=: FALSE if you do not want to print the actual code in your documentinclude: FALSE if you want to suppress code AND output in your document (i.e., run only)warning: FALSE if you want to suppress warning messagesmessage: FALSE if you want to suppress other messagescache: TRUE if you want to store the results of big processing chunks so R does not re-run every time you knitTo set global options, add to the setup chunk:
# knitr::opts_chunk$set(echo = TRUE) # original
knitr::opts_chunk$set(echo = TRUE, cache = TRUE, warning = FALSE)To set options by chunk, add a comma after your chunk name (names are optional):
{r cars, echo = TRUE, cache = TRUE, warning = FALSE}
{r, echo = TRUE, cache = TRUE, warning = FALSE}You can create objects in chunks and refer to these objects in the markdown. Try this: In the chunk cars, create an object called meanSpeed:
meanSpeed <- mean(cars$speed)Then below and outside of the chunk (not inside as shown here), write:
The mean speed is `r meanSpeed`.The important part is to surround your code with two backticks and begin the expression with r and a space.
Alternatively, you can skip the object step and write the code in-line. (Remember: write this OUTSIDE of the chunk)
The mean speed is `r meanSpeed`.
The mean speed is `r mean(cars$speed)`. It’s helpful to create objects because you often need to re-use them, and in-line expressions can become very long and hard to read.
In the pressure chunk options, start typing “fig.” and you will see several figure-related options:
fig.height= and fig.width=: Can set neither, one, or bothfig.align=: Options are “left”, “right”, and “center”fig.cap=: “This is my figure.”See https://yihui.name/knitr/options/#plots for a full list of options.
There are lots of Rmarkdown templates. Let’s try a template for APA-style manuscripts. Load (install if necessary) the devtools package, and then install the papaja package:
devtools::install_github("crsh/papaja")
Once you’ve installed papaja, open a new Rmarkdown file and select “APA article” from the list in the “From Template” section.
products/manusript (inside products)products/manusriptpapaja author created a very flexible template that makes it easy to configure document options in the YAML. Start by giving your paper a title and shorttitle..bib files. It works, but there are better reference managers.Tools/Add-ons/Extensions and use gear to find and install file you downloaded; restart Zotero.bibbibliography (.bib gets added automatically) into products/manuscriptTake note of the citation keys of the references you want to cite.
bibliography.bib file in RStudio, the citation key will be the first entry after the opening bracketbibliography: ["r-references.bib"] to bibliography: ["bibliography.bib"]r_refs(file = "r-references.bib") to r_refs(file = "bibliography.bib")Try citing some text in the introduction. Note the two main styles.
There is a lot of important literature to cite [@button_power_2013;@ioannidis_why_2005]. @sterne_recommendations_2011 have important things to say.
The mean length is 5.8. Table \@ref(tab:myTable) shows some interesting results. Figure \@ref(fig:myFigure) shows an interesting plot.
myTable. Reference the table in the text with \@ref(tab:myTable)myFigure. Reference the figure in the text with \@ref(fig:myFigure)After naming the chunk, include results = "asis".
knitr::kable(
head(mtcars[, 1:8], 10), booktabs = TRUE,
caption = 'A table of the first 10 rows of the mtcars data.'
)| mpg | cyl | disp | hp | drat | wt | qsec | vs | |
|---|---|---|---|---|---|---|---|---|
| Mazda RX4 | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.620 | 16.46 | 0 |
| Mazda RX4 Wag | 21.0 | 6 | 160.0 | 110 | 3.90 | 2.875 | 17.02 | 0 |
| Datsun 710 | 22.8 | 4 | 108.0 | 93 | 3.85 | 2.320 | 18.61 | 1 |
| Hornet 4 Drive | 21.4 | 6 | 258.0 | 110 | 3.08 | 3.215 | 19.44 | 1 |
| Hornet Sportabout | 18.7 | 8 | 360.0 | 175 | 3.15 | 3.440 | 17.02 | 0 |
| Valiant | 18.1 | 6 | 225.0 | 105 | 2.76 | 3.460 | 20.22 | 1 |
| Duster 360 | 14.3 | 8 | 360.0 | 245 | 3.21 | 3.570 | 15.84 | 0 |
| Merc 240D | 24.4 | 4 | 146.7 | 62 | 3.69 | 3.190 | 20.00 | 1 |
| Merc 230 | 22.8 | 4 | 140.8 | 95 | 3.92 | 3.150 | 22.90 | 1 |
| Merc 280 | 19.2 | 6 | 167.6 | 123 | 3.92 | 3.440 | 18.30 | 1 |
After naming the chunk, include fig.height=3, fig.cap="This is a figure."
library(tidyverse)
ggplot(iris, aes(Sepal.Length)) +
geom_histogram() +
facet_grid(. ~ Species) +
theme_bw()## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
This is a figure.
Change the output from output: papaja::apa6_pdf to output: papaja::apa6_word to create a Word file.